﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Common;

namespace EHR18
{
    public partial class FrmModulesConfig : FrmBase
    {
        string ConfigPath = Path.GetFullPath("./Configs");
        public FrmModulesConfig()
        {
            InitializeComponent();
            dateTimePicker1.Value = DateTime.Parse(GenneralConfig.instance.ExportXlsxTime);
            comboBox1.SelectedIndex = GenneralConfig.instance.IO_TYPE - 1;
            textBox1.Text = GenneralConfig.instance.OutputPath;
            textBox3.Text = GenneralConfig.instance.MachineNo;
        }
        private void UpdateConfigsToList()
        {
            string[] files = Directory.GetFiles(ConfigPath, "*.xml");
            listBox1.Items.Clear();
            if (files.Count() > 0)
            {
                foreach (string file in files)
                {
                    Name = Path.GetFileNameWithoutExtension(file);
                    listBox1.Items.Add(Name);
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string Name = listBox1.Items[(listBox1.SelectedIndex)].ToString();
            string FileName = Path.Combine(ConfigPath, Name + ".xml");
            if (File.Exists(FileName))
            {
                FrmModuleConfig frm = new FrmModuleConfig(Name);
                frm.FormBorderStyle = FormBorderStyle.None;
                //     frm.WindowState = FormWindowState.Maximized;
                frm.Dock = DockStyle.Fill;
                frm.TopLevel = false;
                frm.BringToFront();
                foreach (Control f in panel1.Controls)
                {
                    if (f is Form)
                    {
                        panel1.Controls.Remove(f);
                    }
                }
                panel1.Controls.Add(frm);
                frm.Show();
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            FormAddModule frm = new FormAddModule();
            frm.SaveOK += new Action(UpdateConfigsToList);
            frm.FormBorderStyle = FormBorderStyle.None;
            frm.Dock = DockStyle.Fill;
            //  frm.WindowState = FormWindowState.Maximized;          
            frm.TopLevel = false;
            frm.BringToFront();
            foreach (Control f in panel1.Controls)
            {
                if (f is Form)
                {
                    panel1.Controls.Remove(f);
                }
            }
            this.panel1.Controls.Add(frm);
            frm.Show();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            string Name = listBox1.Items[(listBox1.SelectedIndex)].ToString();
            string FileName = Path.Combine(ConfigPath, Name + ".xml");
            if (File.Exists(FileName))
            {
                File.Delete(FileName);
            }
            UpdateConfigsToList();
        }

        private void FrmModulesConfig_Load(object sender, EventArgs e)
        {
            UpdateConfigsToList();
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            GenneralConfig.instance.IO_TYPE = this.comboBox1.SelectedIndex + 1;
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            GenneralConfig.instance.ExportXlsxTime = this.dateTimePicker1.Value.ToString("HH:mm:ss");
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            GenneralConfig.instance.OutputPath = this.textBox1.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog().Equals(DialogResult.OK))
            {
                textBox1.Text = dialog.SelectedPath;
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            GenneralConfig.instance.MachineNo = this.textBox3.Text;
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {

        }
    }
}
